#include "SubtitleManager.h"
#include <windows.h>
#include <math.h>
#include "glut.h"
#include "random.h"

const float_t PI =3.1416; 

#define WORDS_MAX 5
char8_t *badwords[30] =
{
	"You will never pass it.",
	"Noob!",
	"Now what you do?",
	"Give up, dude!",
	"Even Sahil can beat you."
};

const char8_t* string_Atempt = "Attempt";
int32_t attemptTimes;
char8_t buff[20]; //buff string for attempt times


SubtitleManager* SubtitleManager::sInstance   = NULL;

SubtitleManager* SubtitleManager::CreateInstance()
{
	if(sInstance!=NULL)return sInstance;
	else

		sInstance = new SubtitleManager();
	return sInstance;
}

void SubtitleManager::init()
{
	m_SubtitleList = new list<subtitleInfoT*>();

	//init buff string
	attemptTimes = 0;
	itoa(attemptTimes, buff, 10);
}

void SubtitleManager::Update(float_t millionsecond)
{
	//update the subtitle words
	list<subtitleInfoT*>::iterator it;
	for(it = m_SubtitleList->begin(); it!=m_SubtitleList->end(); )
	{
		subtitleInfoT* _info = *it;

		//change the pos
		_info->posX += _info->speed * cos(PI * (_info->speedAngle) / 180) * millionsecond;
		_info->posY += _info->speed * sin(PI * (_info->speedAngle) / 180) * millionsecond;

		_info->lifeTime -= millionsecond;
		if(_info->lifeTime <= 0)
		{
			m_SubtitleList->erase(it++);
		}
		else
		{
			++it;
		}
	}
}

void SubtitleManager::SetAttemptTimes(int32_t argAttempts)
{
	attemptTimes = argAttempts;
	itoa(attemptTimes, buff, 10);
}

void SubtitleManager::Draw()
{
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();

	//glOrtho(0.0f, 1024.0f, 768.0f, 0.0f, 0.0f, 1.0f);
	glOrtho(-1000.0, 1000.0, -1000.0, 1000.0, -400.0, 400.0);
	glScalef(0.5f, 0.5f, 1.0f);
	glLineWidth(2.5f);

	//draw subtitle words
	//list<subtitleInfoT*>::iterator it;
	//for(it = m_SubtitleList->begin(); it!=m_SubtitleList->end(); ++it)
	//{
	//	glPushMatrix();
	//	//glLoadIdentity();

	//	subtitleInfoT* _info = *it;

	//	glColor3f(_info->r, _info->g, _info->b );
	//	glTranslatef(_info->posX, _info->posY, 0);

	//	//char *c;
	//	//c = (char*)malloc(sizeof(strlen(string)+1));
	//	//strcpy(c, string);
	//	char8_t* c = _info->subString;
	//	for (; *c != '\0'; c++)
	//	{
	//		glutStrokeCharacter(GLUT_STROKE_ROMAN  , *c);
	//	}
	//	glPopMatrix();
	//}

	//draw attempt word
	glPushMatrix();
	glScalef(1.5f, 1.5f, 1.0f);
	glTranslatef(-350, 900, 0);
	const char8_t* c = string_Atempt;
	glColor3f(0, 1, 0);
	for (; *c != '\0'; c++)
	{
		glutStrokeCharacter(GLUT_STROKE_ROMAN  , *c);
	}
	glTranslatef(50, 0, 0);
	
	c = buff;
	for (; *c != '\0'; c++)
	{
		glutStrokeCharacter(GLUT_STROKE_ROMAN  , *c);
	}
	glPopMatrix();

	//gluPerspective(45.0f,(GLfloat)1024/(GLfloat)768,0.1f,100.0f);
	glPopMatrix();
	glMatrixMode (GL_MODELVIEW);
	//glPopMatrix();
}

void SubtitleManager::AddDynamicSubtitle()
{
	subtitleInfoT* _info = new subtitleInfoT();
	_info->subString = badwords[getRangedRandom(0, WORDS_MAX)];
	_info->posX = getRangedRandom(-1000.0f, 500.0f);
	_info->posY = getRangedRandom(-1000.0f, 1000.0f);
	_info->r = getRangedRandom(0.0f, 1.0f);
	_info->g = getRangedRandom(0.0f, 1.0f);
	_info->b = getRangedRandom(0.0f, 1.0f);
	_info->speed = getRangedRandom(-100.0f, 100.0f);
	_info->speedAngle = getRangedRandom(0, 360);
	_info->lifeTime = getRangedRandom(3.0f, 5.0f);
	m_SubtitleList->push_back(_info);
}


void SubtitleManager::IncreaseAttemptTimes(int32_t increase)
{
	attemptTimes += increase;
	itoa(attemptTimes, buff, 10);
}